﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ALAZ.TelephonyEx.Voip.IAX;

namespace Spike
{
    public partial class Form1 : Form
    {
        IAXClientClass iaxc;
        public Form1()
        {
            InitializeComponent();
        }

        #region Events

        public void iaxc_OnLineState(int lineNumber)
        {
            //----- Remote number!
            string remote = iaxc.Lines[lineNumber].RemoteName;

            if (remote == String.Empty)
            {
                label4.Text = "None";
            }
            else
            {
                label4.Text = remote;
            }

            //----- Call status!
            //label4.Text = iaxc.Lines[0].ToString();
            if (iaxc.Lines[lineNumber].Active)
                label2.Text = iaxc.Lines[lineNumber].CallStatus.ToString().Substring(2);


            if (iaxc.Lines[lineNumber].Active)
            {
                //----- Call direction!
                label6.Text = iaxc.Lines[lineNumber].CallDirection.ToString().Substring(2);
            }
            else
            {
                //----- Call time!
                label6.Text = "00:00";
                label2.Text = String.Empty;
            }

            if (iaxc.Lines[lineNumber].CallStatus == CallStatus.csRinging)
            {
                //----- Popup
                Show();
                WindowState = FormWindowState.Normal;
            }

        }


        public void iaxc_OnRegistration()
        {

            if (iaxc.RegistrationRequired)
            {

                //----- If registration required check registration status!
                if (iaxc.RegistrationStatus == RegistrationStatus.rsCompleted)
                {
                    MessageBox.Show("Registado");
                }
                else
                {
                    MessageBox.Show("Erro no Registo");
                }
            }
        }
        #endregion

        private void button3_Click(object sender, EventArgs e)
        {
            iaxc.Lines[0].MakeCall(textBox1.Text.Trim());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            iaxc.Lines[0].AnswerCall();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            iaxc.Lines[0].DropCall();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (sender is Button)
            {
                iaxc.PlayDigit((byte)((Button)sender).Text[0]);
                iaxc.Lines[0].SendDTMF((byte)((Button)sender).Text[0]);
                
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            iaxc = new IAXClientClass(AudioType.atAUDIO_INTERNAL_PA, 2);

            iaxc.OnRegistration += new OnRegistrationDelegate(iaxc_OnRegistration);
            iaxc.OnLineState += new OnLineStateDelegate(iaxc_OnLineState);

            //----- Initialize with 2 lines!
            iaxc.CallerIdName = "Tome";
            iaxc.CallerIdNumber = "17327";
            iaxc.Initialize("17327", "123456", "192.168.0.103", true);


            //----- Initialize formats!
            iaxc.PreferredFormat = AudioFormat.afIAXC_FORMAT_GSM;
            iaxc.AllowedFormats = AudioFormat.afIAXC_FORMAT_GSM;

            //----- Initialize audio devices!
            iaxc.AudioDevices.SelectInputAudioDevice(1);
            iaxc.AudioDevices.SelectOutPutAudioDevice(3);
            //iaxc.AudioDevices.SelectOutPutAudioDevice(3);

            //----- Adjust user and registration message status!
            label1.Text = iaxc.User;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //iaxc.ShutDown();
        }

    }
}
